home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
6984
/
6984.xpi
/
chrome
/
lazarus.jar
/
content
/
background-task.js
< prev
next >
Wrap
Text File
|
2009-11-24
|
1KB
|
46 lines
(function(self){
/**
* run a task in a background thread,
* call callback when complete, or on error
*/
self.backgroundTask = function(func, callback){
//un-fraking-believable, backgroundTasks are no good when Firefox is loading,
//they still prevent the UI from being initalized
//so we'll wrap them in a setTimeout as well.
setTimeout(function(){
//create a new background thread
var returnVal = null;
var completed = false;
var err = null;
var threadObj = {
run: function(){
try {
returnVal = func();
}
catch(err){
Components.utils.reportError(err);
returnVal = false;
}
completed = true;
}
}
var thread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).newThread(0);
thread.dispatch(threadObj, thread.DISPATCH_NORMAL);
var currThread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).currentThread;
while (!completed){
currThread.processNextEvent(true);
}
callback(returnVal);
}, 1);
}
})(Lazarus);